package com.shujia.spark.core

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.rdd.RDD

object Demo15MapValues {
  def main(args: Array[String]): Unit = {
    /**
     * mapValues：作用在KV格式的RDD上，会对每条数据的KV的V进行处理
     */

    val conf = new SparkConf()
    conf.setMaster("local")
    conf.setAppName(this.getClass.getSimpleName.replace("$", ""))
    val sc = new SparkContext(conf)

    val kvRDD: RDD[(String, Int)] = sc.parallelize(List(("a", 1), ("b", 3), ("c", 2)))

    kvRDD
      // 将v放大两倍
      .mapValues(i => i * 2)
      .foreach(println)

    // 等价于
    kvRDD
      // 将v放大两倍
      .map(kv => (kv._1, kv._2 * 2))
      .foreach(println)
  }

}
